<template>
  <div class="notification-container">
    <transition-group name="notification">
      <div
        v-for="notification in notifications"
        :key="notification.id"
        class="notification"
        :class="`notification-${notification.type}`"
      >
        {{ notification.message }}
      </div>
    </transition-group>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'

export default {
  setup() {
    const notifications = ref([])

    const addNotification = (message, type = 'info', timeout = 3000) => {
      const id = Date.now()
      notifications.value.push({ id, message, type })

      if (timeout > 0) {
        setTimeout(() => {
          removeNotification(id)
        }, timeout)
      }
    }

    const removeNotification = (id) => {
      notifications.value = notifications.value.filter(n => n.id !== id)
    }

    onMounted(() => {
      // 暴露方法给全局使用
      window.$notify = addNotification
    })

    return {
      notifications,
      addNotification,
      removeNotification
    }
  }
}
</script>

<style scoped>
.notification-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 9999;
}

.notification {
  padding: 12px 20px;
  margin-bottom: 10px;
  border-radius: 4px;
  color: white;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.notification-info {
  background-color: #3498db;
}

.notification-success {
  background-color: #2ecc71;
}

.notification-warning {
  background-color: #f39c12;
}

.notification-error {
  background-color: #e74c3c;
}

.notification-enter-active,
.notification-leave-active {
  transition: all 0.5s ease;
}

.notification-enter-from,
.notification-leave-to {
  opacity: 0;
  transform
: translateX(30px);
}
</style>
